package cs.rsa.ts14.golf;

import cs.rsa.ts14.framework.LineType;
import cs.rsa.ts14.framework.LineTypeClassifierStrategy;

public class StandardLineTypeClassifierStrategy implements LineTypeClassifierStrategy {

    LineType lastSeen;

    @Override
    public LineType classify(String line) {
        lastSeen = LineType.INVALID_LINE;

        String emptyLinePattern = "^\\s+$";
        if (line.matches(emptyLinePattern)) {
            lastSeen = LineType.EMPTY_LINE;
        }

        String commentLinePattern = "#.*";
        if (line.matches(commentLinePattern)) {
            lastSeen = LineType.COMMENT_LINE;
        }

        String weekPattern = "Week\\s+(([1-4]??[0-9])|([5][1-3]))\\s+:\\s+[0-5]\\s+:\\s+[0-5]\\s*";
        if (line.matches(weekPattern)) {
            lastSeen = LineType.WEEK_SPECIFICATION;
        }

        String assignmentLinePattern = "\\w+\\s+=+\\s+[0-9]+(.[0-9])?";
        if (line.matches(assignmentLinePattern)) {
            lastSeen = LineType.ASSIGNMENT_LINE;
        }

        String weekDaySpecificationLine = "(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\\s+(Bi|Ca|Pu|Tr|No|Ho)\\s+.+";
        if (line.matches(weekDaySpecificationLine)) {
            lastSeen = LineType.WEEKDAY_SPECIFICATION;
        }

        String workSpecificationLine = "\\s+[a-zA-Z]\\w*\\s+(-|[a-zA-Z]\\w*)\\s+((([1]??[0-9])|([2][0-3]))((,0)|(,5))?|24,0)";
        if (line.matches(workSpecificationLine)) {
            lastSeen = LineType.WORK_SPECIFICATION;
        }

        return lastSeen;
    }

    @Override
    public String lastError() {
        if (lastSeen == LineType.INVALID_LINE) return "Invalid weekday: Fre";
        return "No error";
    }

}
